package com.toro.server.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: MyChannelDuplexHandler
 * @Description:
 * @Author: ToroLHZ
 * @Date: 2023/12/12 18:51
 */
@Slf4j
@ChannelHandler.Sharable
public class MyChannelDuplexHandler extends ChannelDuplexHandler {
    /**
     * @Description: 触发特殊事件
     * @Param: [ctx, evt]
     * @Return: void
     * @Author: ToroLHZ
     * @Date:  2023/12/12 18:46
     **/
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            log.debug("60秒没有接受到心跳数据");
            //断开连接
//            ctx.channel().close();
        }
//        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("服务端：{}加入",channel);
        super.handlerAdded(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("客户端：{}离开",channel);
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("客户端：{}上线",channel);
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("客户端：{}下线",channel);
        super.channelInactive(ctx);
    }
}
